//
//  LoginView.m
//  nonam
//
//  Created by liuchengming on 17/11/11.
//  Copyright © 2017年 liuchengming. All rights reserved.
//

#import "LoginView.h"

@interface LoginView ()
@property (strong, nonatomic) IBOutlet UITextField *UesrName;
@property (strong, nonatomic) IBOutlet UITextField *PassWord;

@end

@implementation LoginView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)onLogin:(id)sender
{
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/login.php?name=%@&password=%@",_UesrName.text,_PassWord.text]];
    NSURLRequest * req = [NSURLRequest requestWithURL:url];
    NSURLSession * session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask * task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
    {
        //NSString * text = [[NSString alloc]initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        //NSLog(@"%@",text);
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //大中枢派发
        dispatch_async(dispatch_get_main_queue(), ^
        {
            if ([dict[@"errcode"]intValue] == 0)
            {
                //以modal方式推入页面
                [self dismissViewControllerAnimated:YES completion:^{
                                                                  
                }];
            }else{
                NSString * str = [dict objectForKey:@"errinfo"];
                UIAlertController * aler = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:aler animated:YES completion:^
                                                               {
                                                               }];
                UIAlertAction * action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull  action)
                {
                }];
                [aler addAction:action];
            }
        });
    }];
    [task resume];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
